package com.wiscamp.ninechapters.tags.adapter.resources;

import com.wiscamp.ninechapters.tags.application.dto.command.CreateCourseCommand;
import com.wiscamp.ninechapters.tags.application.dto.command.UpdateCourseCommand;
import com.wiscamp.ninechapters.tags.application.dto.vo.CourseVO;
import com.wiscamp.ninechapters.tags.application.services.CourseAppService;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 课程服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Tags")

@Tag(name = "标签知识点-课程信息")
public class CourseController extends BaseController {

    private final CourseAppService courseAppService;

    public CourseController(CourseAppService courseAppService) {
        this.courseAppService = courseAppService;
    }

    // region course
    @GetMapping("Courses/{courseId}")
    @Operation(summary = "通过课程编号获取课程")
    @HandleResourceException
    public RestResponse<CourseVO> getCourse(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                            @PathVariable("courseId") long courseId) {
        return ResourceUtils.getById(request, response, courseId, courseAppService::getCourse);
    }

    /**
     * 获取课程列表
     *
     * @return 课程列表
     */
    @GetMapping("Courses/Available")
    @Operation(summary = "获取课程列表")
    @HandleResourceException
    public RestResponse<Collection<CourseVO>> getAvailableCourses(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, courseAppService::getAvailableCourses);
    }

    /**
     * 通过课程代码获取课程
     *
     * @param courseCode
     * @return
     */
    @GetMapping("Courses/Code/{courseCode}")
    @Operation(summary = "通过课程代码获取课程")
    @HandleResourceException
    public RestResponse<CourseVO> getCourseByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                  @PathVariable("courseCode") String courseCode) {
        return ResourceUtils.getByText(request, response, courseCode, courseAppService::getCourseByCode);
    }


    /**
     * 通过名称获取课程列表
     *
     * @param courseName
     * @return
     */
    @GetMapping("Courses/Name/{courseName}")
    @Operation(summary = "通过名称获取课程列表")
    @HandleResourceException
    public RestResponse<Collection<CourseVO>> getCoursesByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                               @PathVariable("courseName") String courseName) {
        return ResourceUtils.getListByText(request, response, courseName, courseAppService::getCoursesByName);
    }

    @PostMapping("Courses/Query")
    @Operation(summary = "分页获取课程列表")
    @HandleResourceException
    public RestResponse<Collection<CourseVO>> getCoursesByQuery(@Context HttpServletRequest request,
                                                                @Context HttpServletResponse response, @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, courseAppService::getCoursesByQuery);
    }

    /**
     * 通过课程编号删除课程
     *
     * @param id
     * @return
     */
    @PutMapping("Courses/Actions/Delete")
    @Operation(summary = "删除课程")
    @HandleResourceException
    public RestResponse<Boolean> deleteCourse(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                              @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, courseAppService::deleteCourse);
    }

    @PutMapping("Courses/Actions/Enable")
    @Operation(summary = "启用课程")
    @HandleResourceException
    public RestResponse<Boolean> enableCourse(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                              @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, courseAppService::enableCourse);
    }

    @PutMapping("Courses/Actions/Disable")
    @Operation(summary = "启用课程")
    @HandleResourceException
    public RestResponse<Boolean> disableCourse(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                               @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, courseAppService::disableCourse);
    }


    /**
     * 创建课程
     *
     * @param command
     * @return
     */
    @PostMapping("Courses")
    @Operation(summary = "创建课程")
    @HandleResourceException
    public RestResponse<CourseVO> createCourse(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                               @RequestBody CreateCourseCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, courseAppService::createCourse);
    }

    /**
     * 更新课程
     *
     * @param command
     * @return
     */
    @PutMapping("Courses")
    @Operation(summary = "更新课程")
    @HandleResourceException
    public RestResponse<CourseVO> updateCourse(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                               @RequestBody UpdateCourseCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, courseAppService::updateCourse);
    }
    // endregion


    //region outlines

    //endregion
}
